package com.sf.dlb.web.controller;


import com.sf.dlb.bean.UserGradeBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import com.sf.dlb.web.service.WUserGradeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 等级信息相关
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping(value = "/v1/userGrade")
public class UserGradeController {
    private static Logger logger = LoggerFactory.getLogger(UserGradeController.class);
    @Autowired
    private WUserGradeService userGradeService;
    /**
     * 获取等级信息列表
     *
     * @return
     */
    @GetMapping(value = "/list")
    @ResponseBody
    public Object list(HttpServletRequest request) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        resultVO=userGradeService.list();
        logger.info("list response json " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 添加等级信息
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public Object addUserGrade(HttpServletRequest request,@RequestBody UserGradeBean userGradeBean) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        logger.info("addUserGrade request json === " + GsonUtils.getInstance().toJson(userGradeBean));
        if(StringUtils.isBlank(userGradeBean.getGrade())) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少等级参数");
        }
        if(StringUtils.isBlank(userGradeBean.getName())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少等级名称参数");
        }
        if(StringUtils.isBlank(userGradeBean.getGradeDesc())) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少等级参数");
        }
        if(null==userGradeBean.getPrice()){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少等级价格参数");
        }
        if(null==userGradeBean.getValidity()){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少有效期参数");
        }
        if(null==userGradeBean.getMultiple()){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少阅读倍数参数");
        }
        if(null==userGradeBean.getInvitationNumber()){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少邀请数量参数");
        }
        if(null==userGradeBean.getReadDivide()){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少阅读分成参数");
        }
        if(null==userGradeBean.getBuyDivide()){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少购买分成参数");
        }
        if(null==userGradeBean.getMoneyPoomDivide()){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少资金池分成参数");
        }
        resultVO=userGradeService.add(userGradeBean);
        logger.info("addUserGrade response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 更新等级信息
     */
    @PostMapping(value = "/update")
    @ResponseBody
    public Object updateUserGrade(HttpServletRequest request,@RequestBody UserGradeBean userGradeBean) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        logger.info("updateUserGrade request json === " + GsonUtils.getInstance().toJson(userGradeBean));
        if(null==userGradeBean.getId()) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少id参数");
        }
        resultVO=userGradeService.update(userGradeBean);
        logger.info("updateUserGrade response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 删除等级信息
     */
    @DeleteMapping(value = "/delete/{id}")
    @ResponseBody
    public Object deleteUserGrade(HttpServletRequest request, @PathVariable("id") Long id) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        if(null==id){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少id参数");
        }
        logger.info("deleteUserGrade request json === " + id);
        resultVO=userGradeService.delete(id);
        logger.info("deleteUserGrade response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

}
